package com.lv.common.dialog.manager

/**
 * 作者： ygx
 * 创建日期：2023/2/15 10:08 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */
class DialogChain(
    owner: DialogChainOwner?,
    chainNodes: MutableList<ChainNode>?,
    index: Int
) {
    private var chainOwner: DialogChainOwner?
    private val chainNodes: MutableList<ChainNode>?
    private val index: Int

    init {
        chainOwner = owner
        this.chainNodes = chainNodes
        this.index = index
    }

    fun proceed() {
        if (chainOwner != null && chainOwner!!.isDestroyed) {
            destroy()
            return
        }
        if (chainNodes == null || index >= chainNodes.size) return
        val next = DialogChain(chainOwner, chainNodes, index + 1)
        val chainNode: ChainNode = chainNodes[index]
        if (chainOwner != null && chainOwner!!.isCancelled(chainNode.getId())) {
            next.proceed()
        } else {
            chainNode.proceed(next)
        }
    }

    fun destroy() {
        chainOwner = null
        chainNodes!!.clear()
    }
}
